package org.bronycorps.mlptss.domain.actor;

import org.bronycorps.mlptss.domain.controller.AbstractController;
import org.bronycorps.mlptss.domain.controller.SpecialBulletController;
import org.bronycorps.mlptss.domain.model.AbstractActorModel;
import org.bronycorps.mlptss.domain.model.SpecialBullet;

import com.badlogic.gdx.math.Vector2;

public class SpecialBulletActor extends AbstractActor {
	private boolean alive = true;

	private SpecialBulletController bulletController;

	public SpecialBulletActor(SpecialBulletController bulletController) {
		super();

		this.bulletController = bulletController;

		this.currentAnimation = ((SpecialBullet) getModel()).getAnimation();

		setDrawable(this.currentAnimation.getDrawableKeyFrame(this.currentTime));
		setWidth(getPrefWidth());
		setHeight(getPrefHeight());
		setOrigin(getModelOrigin().x, getModelOrigin().y);

		this.hitBox.setWidth(getBounds().width);
		this.hitBox.setHeight(getBounds().height);

		if (bulletController.getModel().followDirection()) {
			setRotation(this.bulletController.getModel().getAngle());
		}
	}

	@Override
	public void act(float delta) {
		super.act(delta);

		if (getModelPosition().y <= 0 || getModelPosition().y > 300) {
			this.alive = false;
		}

		if (!this.alive || this.bulletController.getLifespan() <= 0) {
			terminate();
		}
	}

	public SpecialBulletController getBullet() {
		return this.bulletController;
	}

	@Override
	protected AbstractController getController() {
		return this.bulletController;
	}

	@Override
	public AbstractActorModel getModel() {
		return this.bulletController.getModel();
	}

	@Override
	protected Vector2 getModelPosition() {
		return getModel().getPosition();
	}

	public boolean isAlive() {
		return this.alive;
	}

	public void setAlive(boolean alive) {
		this.alive = alive;
	}

	public void setInitialPosition(Vector2 position) {
		getModelPosition().set(position);
		updateVisualPosition();
	}

	public void terminate() {
		getTrack().removeBullet(this);
	}

}
